package com.xinqi.modules.course.course.controller.app;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.xinqi.common.base.data.IdNameDTO;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.base.response.R;
import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.common.green.annotation.Text;
import com.xinqi.common.mybatis.utils.Pages;
import com.xinqi.common.satoken.utils.LoginHelper;
import com.xinqi.modules.course.clazz.service.ClassService;
import com.xinqi.modules.course.clazz.dto.req.ClassQueryDTO;
import com.xinqi.modules.course.clazz.dto.rsp.ClassResultDTO;
import com.xinqi.modules.course.course.dto.req.*;
import com.xinqi.modules.course.course.dto.rsp.CourseResultDTO;
import com.xinqi.modules.course.course.dto.rsp.CourseResultNewDTO;
import com.xinqi.modules.course.course.service.CourseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * 课程APP控制层
 *
 * @author: system
 * @date: 2023/01/17
 */
@Tag(name = "App-课程", description = "课程")
@RestController
@SaCheckLogin
@RequestMapping("/app/course")
@RequiredArgsConstructor
public class CourseAppController {

    private final CourseService courseService;
    private final ClassService classService;

    @Operation(summary = "课程表-分页")
    @GetMapping("/page")
    public R<PageInfo<CourseResultDTO>> page(Param pageable, CourseQueryDTO dto) {
        PageInfo<CourseResultDTO> page = courseService.page(pageable, dto);
        return R.data(page);
    }

    @Operation(summary = "我加入的课程-列表")
    @GetMapping("/my-join")
    public R<List<CourseResultDTO>> myJoinList() {
        List<CourseResultDTO> list = courseService.findMyJoinedList();
        return R.data(list);
    }

    @Operation(summary = "我加入的课程-分页")
    @GetMapping("/my-join-page")
    public R<PageInfo<CourseResultDTO>> myJoinPage(Param pageable) {
        PageInfo<CourseResultDTO> list = courseService.findMyJoinedPage(pageable);
        return R.data(list);
    }

    @Operation(summary = "我拥有的课程-列表")
    @GetMapping("/my-create")
    public R<List<CourseResultDTO>> myCreateList() {
        List<CourseResultDTO> list = courseService.findMyCreatedList();
        return R.data(list);
    }

    @Operation(summary = "我拥有的课程-分页")
    @GetMapping("/my-create-page")
    public R<PageInfo<CourseResultDTO>> myCreatePage(Param pageable) {
        PageInfo<CourseResultDTO> list = courseService.findMyCreatedPage(pageable);
        return R.data(list);
    }

    @Operation(summary = "课程表-详情")
    @GetMapping("/{courseId}")
    public R<CourseResultDTO> findById(@PathVariable("courseId") Long id) {
        CourseResultDTO dto = courseService.findById(id);
        return R.data(dto);
    }

    @Operation(summary = "课程-详情-变更")
    @GetMapping("/detail/{courseId}")
    public R<CourseResultNewDTO> findCourseById(@PathVariable("courseId") Long id) {
        CourseResultNewDTO dto = courseService.findDetailById(id);
        return R.data(dto);
    }

    @Operation(summary = "新增课程表")
    @PostMapping
    public R<Long> insert(@Validated @RequestBody CourseCreateDTO dto) {
        return R.data(courseService.insert(dto), "创建成功");
    }

    @Operation(summary = "新增课程--变更")
    @PostMapping("/insert")
    public R<Long> insertCourse(@Validated @RequestBody CourseCreateNewDTO dto) {
        return R.data(courseService.insertCourse(dto), MessageUtils.message("common.add.success"));
    }

    @Operation(summary = "修改课程名称-不更新其他的")
    @PostMapping("/{courseId}/update-name")
    public R<Integer> updateCourseName(@PathVariable("courseId") Long courseId, @Text @NotBlank(message = "缺少课程名称") @RequestParam("courseName") String courseName) {
        return R.data(courseService.updateCourseName(courseId, courseName), "修改成功");
    }

    @Operation(summary = "修改课程表")
    @PutMapping("/{courseId}")
    public R<Boolean> update(@PathVariable("courseId") Long id, @Validated @RequestBody CourseUpdateDTO dto) {
        dto.setId(id);
        return R.data(courseService.updateById(LoginHelper.getXqLoginUser(), dto));
    }

    @Operation(summary = "修改课程--变更")
    @PutMapping("/update/{courseId}")
    public R<Boolean> update(@PathVariable("courseId") Long id, @Validated @RequestBody CourseUpdateNewDTO dto) {
        dto.setId(id);
        return R.data(courseService.updateCourseById(LoginHelper.getXqLoginUser(), dto), "修改成功");
    }

    @Operation(summary = "删除课程表")
    @DeleteMapping("/{courseId}")
    public R<Boolean> delete(@PathVariable("courseId") Long id) {
        return R.data(courseService.deleteById(id));
    }

    @Operation(summary = "判断课程名称是否重复")
    @GetMapping("/is-exists-name")
    public R<Boolean> isExistsByName(IdNameDTO dto) {
        return R.data(courseService.isExistsByName(dto.getName(), dto.getId()));
    }

    @Operation(summary = "查询课程班级")
    @GetMapping("/{courseId}/class")
    public R<List<ClassResultDTO>> list(@PathVariable("courseId") Long courseId) {
        ClassQueryDTO dto = new ClassQueryDTO();
        dto.setCourseId(courseId);
        return R.data(classService.find(dto));
    }

    @Operation(summary = "转让课程-实际是转让班级")
    @PostMapping("/{courseId}/transfer")
    public R<Boolean> transfer(@PathVariable("courseId") Long courseId, @RequestBody CourseTransferDTO dto) {
        dto.setCourseId(courseId);
        dto.setOperationUserId(LoginHelper.getUserId());
        return R.data(courseService.transfer(dto), "转让成功");
    }

    /**
     * 转让课程-加密码校验
     *
     * @author dzy
     */
    @Operation(summary = "转让课程-实际上是转让班级-更新")
    @PostMapping("/{courseId}/transfer/v2")
    public R<Boolean> transferNew(@PathVariable("courseId") Long courseId, @RequestBody CourseTransferV2DTO dto) {
        dto.setCourseId(courseId);
        dto.setOperationUserId(LoginHelper.getUserId());
        return R.data(courseService.transferV2(dto), "转让成功");
    }

    @Operation(summary = "查询课程名称是否可编辑")
    @GetMapping("/{courseId}/is-name-editable")
    public R<Boolean> isNameEditable(@PathVariable("courseId") Long courseId) {
        Boolean boo = courseService.isNameEditable(courseId);
        return R.data(boo);
    }

    @Operation(summary = "解散课程")
    @PostMapping("/{courseId}/dissolve")
    public R<Boolean> dissolve(@PathVariable("courseId") Long courseId, @RequestParam("smsCode") String smsCode, @RequestParam("mobile") String mobile, @RequestBody List<Long> classIdList) {
        return R.data(courseService.dissolve(courseId, smsCode, mobile, classIdList, LoginHelper.getXqLoginUser()), "解散成功");
    }

    @Operation(summary = "置顶课程")
    @PostMapping("/{courseId}/up")
    public R<Boolean> up(@PathVariable("courseId") Long courseId) {
        return R.data(courseService.up(courseId));
    }

    @Operation(summary = "取消置顶")
    @PostMapping("/{courseId}/down")
    public R<Boolean> down(@PathVariable("courseId") Long courseId) {
        return R.data(courseService.down(courseId));
    }

    @Operation(summary = "我的加入-置顶")
    @PostMapping("/{courseId}/my-join-up")
    public R<Boolean> myJoinUp(@PathVariable("courseId") Long courseId) {
        return R.data(courseService.myJoinUp(courseId));
    }

    @Operation(summary = "我的创建-置顶")
    @PostMapping("/{courseId}/my-create-up")
    public R<Boolean> myCreateUp(@PathVariable("courseId") Long courseId) {
        return R.data(courseService.myCreateUp(courseId));
    }

    @Operation(summary = "我的加入-取消置顶")
    @PostMapping("/{courseId}/my-join-down")
    public R<Boolean> myJoinDown(@PathVariable("courseId") Long courseId) {
        return R.data(courseService.myJoinDown(courseId));
    }

    @Operation(summary = "我的创建-取消置顶")
    @PostMapping("/{courseId}/my-create-down")
    public R<Boolean> myCreateDown(@PathVariable("courseId") Long courseId) {
        return R.data(courseService.myCreateDown(courseId));
    }

    @Operation(summary = "我的加入-归档自己")
    @PostMapping("/{courseId}/my-join-archive-self")
    public R<Boolean> myJoinArchiveSelf(@PathVariable("courseId") Long courseId) {
        return R.data(courseService.myJoinArchiveSelf(courseId), "归档成功");
    }

    @Operation(summary = "我的创建-归档自己")
    @PostMapping("/{courseId}/my-create-archive-self")
    public R<Boolean> myCreateArchiveSelf(@PathVariable("courseId") Long courseId) {
        return R.data(courseService.myCreateArchiveSelf(courseId), "归档成功");
    }

    @Operation(summary = "我的加入-归档恢复自己")
    @PostMapping("/{courseId}/my-join-recover-self")
    public R<Boolean> myJoinRecoverSelf(@PathVariable("courseId") Long courseId) {
        return R.data(courseService.myJoinRecoverSelf(courseId), "恢复成功");
    }

    @Operation(summary = "我的创建-归档恢复自己")
    @PostMapping("/{courseId}/my-create-recover-self")
    public R<Boolean> myCreateRecoverSelf(@PathVariable("courseId") Long courseId) {
        return R.data(courseService.myCreateRecoverSelf(courseId), "恢复成功");
    }

    @Operation(summary = "归档全课程(全班)")
    @PostMapping("/{courseId}/archive-all")
    public R<Boolean> archiveAll(@PathVariable("courseId") Long courseId) {
        return R.data(courseService.archiveAll(courseId), "归档成功");
    }

    @Operation(summary = "归档自己")
    @PostMapping("/{courseId}/archive-self")
    public R<Boolean> archiveSelf(@PathVariable("courseId") Long courseId) {
        return R.data(courseService.archiveSelf(courseId), "归档成功");
    }

    @Operation(summary = "归档恢复全部")
    @PostMapping("/{courseId}/recover-all")
    public R<Boolean> recoverAll(@PathVariable("courseId") Long courseId) {
        return R.data(courseService.recoverAll(courseId));
    }

    @Operation(summary = "归档恢复自己")
    @PostMapping("/{courseId}/recover-self")
    public R<Boolean> recoverSelf(@PathVariable("courseId") Long courseId) {
        return R.data(courseService.recoverSelf(courseId));
    }

    @Operation(summary = "归档管理列表-我创建的")
    @GetMapping("/my-created-archive-page")
    public R<PageInfo<CourseResultDTO>> createdArchiveList(Param pageable) {
        return R.data(courseService.createdArchivePageList(Pages.page(pageable)));
    }

    @Operation(summary = "归档管理列表-我加入的")
    @GetMapping("/my-joined-archive-page")
    public R<PageInfo<CourseResultDTO>> joinedArchiveList(Param pageable) {
        return R.data(courseService.joinedArchivePageList(Pages.page(pageable)));
    }

    @Operation(summary = "我加入的课程-详情")
    @GetMapping("/{courseId}/my-join")
    public R<CourseResultDTO> findMyJoinedById(@PathVariable("courseId") Long id) {
        CourseResultDTO dto = courseService.findMyJoinedById(id);
        return R.data(dto);
    }

    @Operation(summary = "我创建的课程-详情")
    @GetMapping("/{courseId}/my-create")
    public R<CourseResultDTO> findMyCreatedById(@PathVariable("courseId") Long id) {
        CourseResultDTO dto = courseService.findMyCreatedById(id);
        return R.data(dto);
    }

    @Operation(summary = "根据班级id-查询我创建的详情")
    @GetMapping("/{classId}/my-create-detail")
    public R<CourseResultDTO> findMyCreatedByClassId(@PathVariable("classId") Long classId) {
        CourseResultDTO dto = courseService.findMyCreatedByClassId(classId);
        return R.data(dto);
    }

    @Operation(summary = "根据班级id-查询我加入的详情")
    @GetMapping("/{classId}/my-join-detail")
    public R<CourseResultDTO> findMyJoinedByClassId(@PathVariable("classId") Long classId) {
        CourseResultDTO dto = courseService.findMyJoinedByClassId(classId);
        return R.data(dto);
    }

    @Operation(summary = "我创建的其他课程")
    @GetMapping("/{courseId}/my-created-other-course")
    public R<List<CourseResultDTO>> myCreatedOtherCourse(@PathVariable("courseId") Long courseId) {
        return R.data(courseService.myCreatedOtherCourse(courseId));
    }
}
